package com.equipment.fkdcn.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.equipment.fkdcn.pojo.EUser;
import com.equipment.fkdcn.service.EUserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;


@Controller
@RequestMapping("/admin")
public class EUserController {

    @Autowired
    @Qualifier("eUserService")
    private EUserServiceImpl userService;



    @PostMapping("/login")
    @ResponseBody//不走页面，表示ajax
    public  String login(@RequestBody String data, HttpServletRequest req, HttpServletResponse res){
        JSONObject jsonObject = JSON.parseObject(data);
        Map<String,Object> map = (Map<String,Object>)jsonObject;
        System.out.println(map);
        List<EUser> login = userService.login(map);
        boolean flag=false;
        if (login.size()>0){
            HttpSession session = req.getSession();
            session.setAttribute("adminName",login.get(0).getRealName());
            flag=true;
        }
        return  "{\"state\":"+flag+"}";
    }





    @PostMapping("/vfAd")
    @ResponseBody
    public String vfAdmin(@RequestBody String data){//检查账号
        JSONObject jsonObject = JSON.parseObject(data);
        String admin = jsonObject.get("admin").toString();
        System.out.println(admin);
        String s = userService.idQuery(admin);
        boolean flag=false;
        if (s!=null) flag=true;
        return "{\"state\":"+flag+"}";
    }

    @PostMapping("/addUser")
    @ResponseBody
    public String addUser(@RequestBody String data){//添加用户
        JSONObject jsonObject = JSON.parseObject(data);
        EUser eUser = JSON.toJavaObject(jsonObject, EUser.class);
        boolean b = userService.addUser(eUser);
        return  "{\"state\":"+b+"}";
    }

}
